package com.zhongli.devplatform.controller.sys;

import com.zhongli.devplatform.dto.sys.DictionaryItemDTO;
import com.zhongli.devplatform.query.sys.DictionaryItemQuery;
import com.zhongli.devplatform.service.imp.sys.IDictionaryDirectoryService;
import com.zhongli.devplatform.service.imp.sys.IDictionaryItemService;
import com.zhongli.devplatform.vo.sys.DictionaryItemVO;
import com.zhongli.devplatform.bean.Res;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.CacheManager;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/dictionaryItem")
public class DictionaryItemController {

    @Autowired
    private CacheManager cacheManager;

    @Autowired
    private IDictionaryItemService dictionaryItemService;

    @Value("${dev-platform.cache-name}")
    private String cacheName;

    @RequestMapping("/listPage")
    public Res listPage(DictionaryItemQuery query) {
        return new Res(dictionaryItemService.listPage(query));
    }

    @PostMapping("/save")
    public Res save(DictionaryItemDTO dictionaryItemDTO) {
        dictionaryItemService.save(dictionaryItemDTO);
        return new Res(null);
    }

    @RequestMapping("/delete")
    public Res delete(@RequestBody List<Long> ids) {
        dictionaryItemService.delete(ids);
        return new Res(null);
    }

    @RequestMapping("/listCache")
    public Res listCache(String code) {
        List<DictionaryItemVO> dictionaryItemVOList = (List<DictionaryItemVO>) cacheManager.getCache(cacheName + IDictionaryDirectoryService.CACHE_KEY).get(code).get();
        return new Res(dictionaryItemVOList);
    }

    @PostMapping("/isExist")
    public Res isExist(DictionaryItemQuery query) {
        return new Res(dictionaryItemService.isExist(query));
    }


}
